/*
 * host.h
 *
 *  Created on: Oct 16, 2024
 *      Author: Jinming
 */

#ifndef INC_HOST_H_
#define INC_HOST_H_

#include "main.h"
#include "motor_task.h"

enum MY_OEM_FLAG
{
    MY_OEM_FLAG_QUERY_READ_JOINT = 0x1234,         // 只为了查询spi数据，不发送can包；针对电机
    MY_OEM_FLAG_QUERY_READ_FORCE_SENSOR = 0x1235,  // 只为了查询spi数据，不发送can包；针对拉张力传感器
    MY_OEM_FLAG_SERVO_ENABLE_START = 0x7CE0,  // 0x7CE0 - 0x7CE2 表示使能第 0 - 2 个电机
    MY_OEM_FLAG_SERVO_DISABLE_START = 0x7CF0, // 0x7CF0 - 0x7CF2 表示失能第 0 - 2 个电机
};

typedef enum {
	CAN_PACKET_SET_NONE = 0,         //未使用
	CAN_PACKET_SET_IDLE,         	//空闲模式
	CAN_PACKET_SET_CURRENT,         //电流环模式
	CAN_PACKET_SET_CURRENT_BRAKE,   // 电流刹车模式
	CAN_PACKET_SET_RPM,             // 转速模式
	CAN_PACKET_SET_POS,              // 位置模式
	CAN_PACKET_SET_POS_SPD,          //位置速度环模式
}CAN_PACKET_ID_e;

/* 该核心连接的子设备 */
typedef enum
{
	SUB_DEV_NONE = 0,				//无
	SUB_DEV_JOINT,					//电机
	SUB_DEV_SENSOR,				//拉力传感器

	SUB_DEV_QUERY_JOINT,
	SUB_DEV_QUERY_SENSOR,
}SUB_DEV_e;

struct __attribute__((packed)) spi_motor_data_t
{
    int32_t pos;
    int32_t speed;
    int32_t current;
    int16_t temp;
    int16_t errCode;
};

struct __attribute__((packed)) spi_sensor_data_t
{
	int32_t force[6];
	int16_t plantarL[12];		//四路三轴传感器，[x,y,z] * 4 = 12
	int16_t plantarR[12];		//四路三轴传感器，[x,y,z] * 4 = 12
};

/*!
 * SPI data message
 */
typedef struct {    // 由于有大小端转换，这里不能使用单字节的类型
    struct spi_motor_data_t motor[4];
    struct spi_sensor_data_t sensor;         //六通道拉力传感器
} spine_data_t;

struct __attribute__((packed)) spi_motor_command_t
{
    int32_t pos;
    int32_t speed;
    int32_t acc;
    int32_t kp;
    int32_t kd;
    int32_t ki;
    int32_t torque;
    int16_t type;               //指令类型，包括控制模式等
};           //电机指令

struct __attribute__((packed)) spi_sensor_command_t
{
	int32_t force[6];
	int16_t plantarL[12];		//四路三轴传感器，[x,y,z] * 4 = 12
	int16_t plantarR[12];		//四路三轴传感器，[x,y,z] * 4 = 12
    int32_t reserve[2];         //预留
};          //传感器指令

typedef struct
{
	uint8_t head;			//头码
	uint8_t len;
	uint8_t cmdType;
	union
	{
		struct spi_motor_command_t motor[4];
		struct spi_sensor_command_t sensor;
	};
}spi_command_t;

int host_task_init(void);
void host_to_spi_sync(MotorInfo_t *motorInfo, uint8_t index);
JointCtrl_t *joint_command_get(void);
void joint_command_clear(void);
void host_lost_timerout(void);

#endif /* INC_HOST_H_ */
